<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: The Array class and coefficient-wise operations</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialArrayClass.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">The Array class and coefficient-wise operations<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>This page aims to provide an overview and explanations on how to use <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>'s <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> class.</p>
<h1><a class="anchor" id="TutorialArrayClassIntro"></a>
What is the Array class?</h1>
<p>The <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> class provides general-purpose arrays, as opposed to the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> class which is intended for linear algebra. Furthermore, the <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> class provides an easy way to perform coefficient-wise operations, which might not have a linear algebraic meaning, such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.</p>
<h1><a class="anchor" id="TutorialArrayClassTypes"></a>
Array types</h1>
<p><a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> is a class template taking the same template parameters as <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>. As with <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>, the first three template parameters are mandatory: </p><div class="fragment"><div class="line">Array&lt;typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime&gt;</div>
</div><!-- fragment --><p> The last three template parameters are optional. Since this is exactly the same as for <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>, we won't explain it again here and just refer to <a class="el" href="group__TutorialMatrixClass.html">The Matrix class</a>.</p>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> also provides typedefs for some common cases, in a way that is similar to the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> typedefs but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays. We adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are the size and the scalar type, as in the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> typedefs explained on <a class="el" href="group__TutorialMatrixClass.html">this page</a>. For 2-dimensional arrays, we use typedefs of the form ArrayNNt. Some examples are shown in the following table:</p>
<table class="manual">
<tr>
<th>Type  </th><th>Typedef   </th></tr>
<tr>
<td><div class="fragment"><div class="line">Array&lt;float,Dynamic,1&gt; </div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">ArrayXf </div>
</div><!-- fragment -->   </td></tr>
<tr>
<td><div class="fragment"><div class="line">Array&lt;float,3,1&gt; </div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Array3f </div>
</div><!-- fragment -->   </td></tr>
<tr>
<td><div class="fragment"><div class="line">Array&lt;double,Dynamic,Dynamic&gt; </div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">ArrayXXd </div>
</div><!-- fragment -->   </td></tr>
<tr>
<td><div class="fragment"><div class="line">Array&lt;double,3,3&gt; </div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">Array33d </div>
</div><!-- fragment -->   </td></tr>
</table>
<h1><a class="anchor" id="TutorialArrayClassAccess"></a>
Accessing values inside an Array</h1>
<p>The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices. Furthermore, the <code>&lt;&lt;</code> operator can be used to initialize arrays (via the comma initializer) or to print them.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXXf</a>  m(2,2);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// assign some values coefficient by coefficient</span></div>
<div class="line">  m(0,0) = 1.0; m(0,1) = 2.0;</div>
<div class="line">  m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);</div>
<div class="line">  </div>
<div class="line">  <span class="comment">// print values to standard output</span></div>
<div class="line">  std::cout &lt;&lt; m &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// using the comma-initializer is also allowed</span></div>
<div class="line">  m &lt;&lt; 1.0,2.0,</div>
<div class="line">       3.0,4.0;</div>
<div class="line">     </div>
<div class="line">  <span class="comment">// print values to standard output</span></div>
<div class="line">  std::cout &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1Array_html"><div class="ttname"><a href="classEigen_1_1Array.html">Eigen::Array</a></div><div class="ttdoc">General-purpose arrays with easy API for coefficient-wise operations.</div><div class="ttdef"><b>Definition:</b> Array.h:49</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">1 2
3 5

1 2
3 4
</pre> </td></tr>
</table>
<p>For more information about the comma initializer, see <a class="el" href="group__TutorialAdvancedInitialization.html">Advanced initialization</a>.</p>
<h1><a class="anchor" id="TutorialArrayClassAddSub"></a>
Addition and subtraction</h1>
<p>Adding and subtracting two arrays is the same as for matrices. The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.</p>
<p>Arrays also support expressions of the form <code>array + scalar</code> which add a scalar to each coefficient in the array. This provides a functionality that is not directly available for <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> objects.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXXf</a> a(3,3);</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXXf</a> b(3,3);</div>
<div class="line">  a &lt;&lt; 1,2,3,</div>
<div class="line">       4,5,6,</div>
<div class="line">       7,8,9;</div>
<div class="line">  b &lt;&lt; 1,2,3,</div>
<div class="line">       1,2,3,</div>
<div class="line">       1,2,3;</div>
<div class="line">       </div>
<div class="line">  <span class="comment">// Adding two arrays</span></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a + b = &quot;</span> &lt;&lt; std::endl &lt;&lt; a + b &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">  <span class="comment">// Subtracting a scalar from an array</span></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a - 2 = &quot;</span> &lt;&lt; std::endl &lt;&lt; a - 2 &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">a + b = 
 2  4  6
 5  7  9
 8 10 12

a - 2 = 
-1  0  1
 2  3  4
 5  6  7
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialArrayClassMult"></a>
Array multiplication</h1>
<p>First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays are fundamentally different from matrices, is when you multiply two together. Matrices interpret multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two arrays can be multiplied if and only if they have the same dimensions.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXXf</a> a(2,2);</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXXf</a> b(2,2);</div>
<div class="line">  a &lt;&lt; 1,2,</div>
<div class="line">       3,4;</div>
<div class="line">  b &lt;&lt; 5,6,</div>
<div class="line">       7,8;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a * b = &quot;</span> &lt;&lt; std::endl &lt;&lt; a * b &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">a * b = 
 5 12
21 32
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialArrayClassCwiseOther"></a>
Other coefficient-wise operations</h1>
<p>The <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> class defines other coefficient-wise operations besides the addition, subtraction and multiplication operators described above. For example, the <a class="el" href="">.abs() </a> method takes the absolute value of each coefficient, while <a class="el" href="">.sqrt() </a> computes the square root of the coefficients. If you have two arrays of the same size, you can call <a class="el" href="">.min(.) </a> to construct the array whose coefficients are the minimum of the corresponding coefficients of the two given arrays. These operations are illustrated in the following example.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXf</a> a = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::ArrayXf::Random</a>(5);</div>
<div class="line">  a *= 2;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a =&quot;</span> &lt;&lt; std::endl</div>
<div class="line">            &lt;&lt; a &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a.abs() =&quot;</span> &lt;&lt; std::endl</div>
<div class="line">            &lt;&lt; a.abs() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a.abs().sqrt() =&quot;</span> &lt;&lt; std::endl</div>
<div class="line">            &lt;&lt; a.abs().sqrt() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;a.min(a.abs().sqrt()) =&quot;</span> &lt;&lt; std::endl</div>
<div class="line">            &lt;&lt; a.min(a.abs().sqrt()) &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:114</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">a =
  1.36
-0.422
  1.13
  1.19
  1.65
a.abs() =
 1.36
0.422
 1.13
 1.19
 1.65
a.abs().sqrt() =
1.17
0.65
1.06
1.09
1.28
a.min(a.abs().sqrt()) =
  1.17
-0.422
  1.06
  1.09
  1.28
</pre> </td></tr>
</table>
<p>More coefficient-wise operations can be found in the <a class="el" href="group__QuickRefPage.html">Quick reference guide</a>.</p>
<h1><a class="anchor" id="TutorialArrayClassConvert"></a>
Converting between array and matrix expressions</h1>
<p>When should you use objects of the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> class and when should you use objects of the <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> class? You cannot apply <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> operations on arrays, or <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> operations on matrices. Thus, if you need to do linear algebraic operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> and <a class="el" href="classEigen_1_1Array.html" title="General-purpose arrays with easy API for coefficient-wise operations.">Array</a> operations. In that case, you need to convert a matrix to an array or reversely. This gives access to all operations regardless of the choice of declaring objects as arrays or as matrices.</p>
<p><a class="el" href="classEigen_1_1MatrixBase.html">Matrix expressions </a> have an <a class="el" href="classEigen_1_1MatrixBase.html#a354c33eec32ceb4193d002f4d41c0497">.array() </a> method that 'converts' them into <a class="el" href="classEigen_1_1ArrayBase.html">array expressions</a>, so that coefficient-wise operations can be applied easily. Conversely, <a class="el" href="classEigen_1_1ArrayBase.html">array expressions </a> have a <a class="el" href="classEigen_1_1ArrayBase.html#af01e9ea8087e390af8af453bbe4c276c">.matrix() </a> method. As with all <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> expression abstractions, this doesn't have any runtime cost (provided that you let your compiler optimize). Both <a class="el" href="classEigen_1_1MatrixBase.html#a354c33eec32ceb4193d002f4d41c0497">.array() </a> and <a class="el" href="classEigen_1_1ArrayBase.html#af01e9ea8087e390af8af453bbe4c276c">.matrix() </a> can be used as rvalues and as lvalues.</p>
<p>Mixing matrices and arrays in an expression is forbidden with <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>. For instance, you cannot add a matrix and array directly; the operands of a <code>+</code> operator should either both be matrices or both be arrays. However, it is easy to convert from one to the other with <a class="el" href="classEigen_1_1MatrixBase.html#a354c33eec32ceb4193d002f4d41c0497">.array() </a> and <a class="el" href="classEigen_1_1ArrayBase.html#af01e9ea8087e390af8af453bbe4c276c">.matrix()</a>. The exception to this rule is the assignment operator: it is allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix variable.</p>
<p>The following example shows how to use array operations on a <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> object by employing the <a class="el" href="classEigen_1_1MatrixBase.html#a354c33eec32ceb4193d002f4d41c0497">.array() </a> method. For example, the statement <code>result = m.array() * n.array()</code> takes two matrices <code>m</code> and <code>n</code>, converts them both to an array, uses to multiply them coefficient-wise and assigns the result to the matrix variable <code>result</code> (this is legal because <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> allows assigning array expressions to matrix variables).</p>
<p>As a matter of fact, this usage case is so common that <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> provides a <a class="el" href="">const .cwiseProduct(.) </a> method for matrices to compute the coefficient-wise product. This is also shown in the example program.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">Eigen::MatrixXf</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> m(2,2);</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> n(2,2);</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> result(2,2);</div>
<div class="line"> </div>
<div class="line">  m &lt;&lt; 1,2,</div>
<div class="line">       3,4;</div>
<div class="line">  n &lt;&lt; 5,6,</div>
<div class="line">       7,8;</div>
<div class="line"> </div>
<div class="line">  result = m * n;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- Matrix m*n: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  result = m.array() * n.array();</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- Array m*n: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  result = m.cwiseProduct(n);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- With cwiseProduct: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  result = m.array() + 4;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- Array m + 4: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">}</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga731599f782380312960376c43450eb48"><div class="ttname"><a href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">Eigen::MatrixXf</a></div><div class="ttdeci">Matrix&lt; float, Dynamic, Dynamic &gt; MatrixXf</div><div class="ttdoc">Dynamic×Dynamic matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">-- Matrix m*n: --
19 22
43 50

-- Array m*n: --
 5 12
21 32

-- With cwiseProduct: --
 5 12
21 32

-- Array m + 4: --
5 6
7 8

</pre> </td></tr>
</table>
<p>Similarly, if <code>array1</code> and <code>array2</code> are arrays, then the expression <code>array1.matrix() * array2.matrix()</code> computes their matrix product.</p>
<p>Here is a more advanced example. The expression <code>(m.array() + 4).matrix() * m</code> adds 4 to every coefficient in the matrix <code>m</code> and then computes the matrix product of the result with <code>m</code>. Similarly, the expression <code>(m.array() * n.array()).matrix() * m</code> computes the coefficient-wise product of the matrices <code>m</code> and <code>n</code> and then the matrix product of the result with <code>m</code>.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">Eigen::MatrixXf</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> m(2,2);</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> n(2,2);</div>
<div class="line">  <a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> result(2,2);</div>
<div class="line"> </div>
<div class="line">  m &lt;&lt; 1,2,</div>
<div class="line">       3,4;</div>
<div class="line">  n &lt;&lt; 5,6,</div>
<div class="line">       7,8;</div>
<div class="line">  </div>
<div class="line">  result = (m.array() + 4).matrix() * m;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- Combination 1: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  result = (m.array() * n.array()).matrix() * m;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;-- Combination 2: --\n&quot;</span> &lt;&lt; result &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">-- Combination 1: --
23 34
31 46

-- Combination 2: --
 41  58
117 170

</pre> </td></tr>
</table>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
